package org.vectrics.skill.struts;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.skill.SkillException;
import org.vectrics.skill.SkillRatingSet;
import org.vectrics.struts.Request;


/**
 * @author mmoore
 */
public class SkillWebUtils {
	private transient static Logger log = Logger.getLogger(SkillWebUtils.class);
	public static String RATING_SESSION_KEY = "ratings_edit";
	public static String RATING_LEVEL_SESSION_KEY = "ratings_level";
	public static String SKILL_SECTION_SESSION_KEY = "skill_section";
	

	static public int getLevel(Request request) {
		log.debug("getLevel() - method to find the skill hierarchy level currently being entered");
		
		Integer levelInt = new Integer(0);
		levelInt = (Integer)request.getSessionAttribute(RATING_LEVEL_SESSION_KEY);
		if (levelInt == null) {
			log.error("Error, Level was not set on session");
		}
		int level = levelInt.intValue();
		return(level);
	}
	
	static public void setLevel(Request request, int level) {
		log.debug("setLevel() - method to set the skill hierarchy level currently being entered");
		Integer levelInt = new Integer(level);
		request.setSessionAttribute(RATING_LEVEL_SESSION_KEY, levelInt);
	}

	static public String getSkillSection(Request request) {
		log.debug("getLevel() - method to find the skill hierarchy level currently being entered");
		
		String skillSection = (String)request.getSessionAttribute(SKILL_SECTION_SESSION_KEY);
		return(skillSection);
	}
	
	static public void setSkillSection(Request request, String skillCode) {
		log.debug("setLevel() - method to set the skill hierarchy level currently being entered");
		request.setSessionAttribute(SKILL_SECTION_SESSION_KEY, skillCode);
	}
	
	static public SkillRatingSet getSkillRatingSet(Request request) {
		log.debug("getSkillRatingSet() - method to get the skill ratings set currently being edited");
		SkillRatingSet set = (SkillRatingSet)request.getSessionAttribute(RATING_SESSION_KEY);
		if (set == null) {
			throw new SkillException("SkillRatingSet was not previously attached to session attribute - make sure PrepareWizardAction is called first");
		}
		return(set);
	}

	static public void setSkillRatingSet(Request request, SkillRatingSet skillRatingSet) {
		if (skillRatingSet == null)
			throw new SystemException("Null skillRating set passed");
		log.debug("setSkillRatingSet() - method to set the skill ratings set currently being edited");
		request.setSessionAttribute(RATING_SESSION_KEY, skillRatingSet);
	}
	
	static public void removeSessionSkillRatingSet(Request request) {
		log.debug("setSkillRatingSet() - method to set the skill ratings set currently being edited");
		request.removeSessionAttribute(RATING_SESSION_KEY);
	}
	
	
}
